//
//  MyViews.m
//  XiangMu
//
//  夏翠萍
//  Created by GERRY－PC on 16/5/5.
//  Copyright © 2016年 GERRY－PC. All rights reserved.
//

#import "MyViews.h"
@interface MyViews ()
{
    //保存所有路径
    NSMutableArray *allPathArray;
    //保存一条路径的所有的点
    NSMutableArray *pointArray;
    //
    NSMutableDictionary *pathDic;
    //
    CGContextRef context;
}


@end

@implementation MyViews
- (void)drawRect:(CGRect)rect
{
    if (!allPathArray) {
        context = UIGraphicsGetCurrentContext();
        allPathArray = [[NSMutableArray alloc]initWithCapacity:0];
        return;
    }
    for (int i=0; i<allPathArray.count; i++) {
        NSDictionary *dic = [allPathArray objectAtIndex:i];
        NSInteger type = [[dic objectForKey:@"type"]integerValue];
        switch (type) {
            case 0:
                [self draw_Line:dic];
                break;
            case 1:
                [self draw_Yuan:dic];
                break;
            case 2:
                [self draw_Pentacle:dic];
                break;
            case 3:
                [self draw_Rectangle:dic];
                break;
            case 4:
                [self draw_Line1:dic];
                break;
            case 5:
                [self XiangPiCa:dic];
                break;
            default:
                break;
        }
    }
}

- (void)qingKong
{
    [allPathArray removeAllObjects];
    [self setNeedsDisplay];
}
//自由绘画
- (void)draw_Line:(NSDictionary *)dic
{
    //画笔颜色
    CGContextSetStrokeColorWithColor(context, [[dic objectForKey:@"color"]CGColor]);
    //画笔大小
    CGContextSetLineWidth(context, [[dic objectForKey:@"width"]floatValue]);

    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint startPoint = [[arrpoint objectAtIndex:0]CGPointValue];
    //设置线段的起始点
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    for (int i=1; i<arrpoint.count; i++) {
        CGPoint points= [[arrpoint objectAtIndex:i]CGPointValue];
        //循环添加点
        CGContextAddLineToPoint(context, points.x, points.y);
    }
    CGContextDrawPath(context, kCGPathStroke);
    
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
}

//画圆
- (void)draw_Yuan:(NSDictionary *)dic
{
    //画笔颜色
    CGContextSetStrokeColorWithColor(context, [[dic objectForKey:@"color"]CGColor]);
    //画笔大小
    CGContextSetLineWidth(context, [[dic objectForKey:@"width"]floatValue]);
    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint pointOne = [[arrpoint objectAtIndex:0] CGPointValue];
    CGPoint pointTwo = [[arrpoint lastObject] CGPointValue];
    //    r*r = x*X +Y*Y圆半径
    CGFloat r = sqrt((pow(pointTwo.x-pointOne.x, 2)+pow(pointTwo.y-pointOne.y, 2)));
    //    圆弧路径
    CGContextAddArc(context, (pointOne.x+pointTwo.x)/2, (pointOne.y+pointTwo.y)/2, r, 0, 2*M_PI, 1);
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
}

//绘制五角星
- (void)draw_Pentacle:(NSDictionary *)dic
{
    CGPoint center;
    //画笔颜色
    CGContextSetStrokeColorWithColor(context, [[dic objectForKey:@"color"]CGColor]);
    //画笔大小
    CGContextSetLineWidth(context, [[dic objectForKey:@"width"]floatValue]);
    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint pointOne = [[arrpoint objectAtIndex:0] CGPointValue];
    CGPoint pointTwo = [[arrpoint lastObject] CGPointValue];

     CGFloat r = sqrt((pow(pointTwo.x-pointOne.x, 2)+pow(pointTwo.y-pointOne.y, 2)));
    center = CGPointMake(pointOne.x, pointTwo.y);
    CGContextMoveToPoint(context, center.x, center.y-r);
    for (int i = 1; i<6; i++) {
        CGFloat x = -(r * sin(i*4.0 * M_PI/5.0));
        CGFloat y = -(r * cos(i*4.0 * M_PI/5.0));
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
}

//绘制矩形
- (void)draw_Rectangle:(NSDictionary *)dic
{
    //画笔颜色
    CGContextSetStrokeColorWithColor(context, [[dic objectForKey:@"color"]CGColor]);
    //画笔大小
    CGContextSetLineWidth(context, [[dic objectForKey:@"width"]floatValue]);
    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint pointOne = [[arrpoint objectAtIndex:0] CGPointValue];
    CGPoint pointTwo = [[arrpoint lastObject] CGPointValue];

    CGContextSetLineCap(context, kCGLineCapSquare);
    //设置拐角处样式
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGFloat r = sqrt((pow(pointTwo.x-pointOne.x, 2)+pow(pointTwo.y-pointOne.y, 2)));
    
    CGContextAddRect(context, CGRectMake(pointOne.x, pointOne.y, r, r));
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
    
}

//自由绘画
- (void)draw_Line1:(NSDictionary *)dic
{
    //画笔颜色
    CGContextSetStrokeColorWithColor(context, [[dic objectForKey:@"color"]CGColor]);
    //画笔大小
    CGContextSetLineWidth(context, [[dic objectForKey:@"width"]floatValue]);

    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint pointOne = [[arrpoint objectAtIndex:0] CGPointValue];
    CGPoint pointTwo = [[arrpoint lastObject] CGPointValue];
    //设置线段的起始点
    CGContextMoveToPoint(context, pointOne.x, pointOne.y);
    
    CGContextAddLineToPoint(context, pointTwo.x, pointTwo.y);
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
}


//橡皮擦
- (void)XiangPiCa:(NSDictionary *)dic
{
    //设置画笔颜色
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);
    //设置线段的宽度
    CGContextSetLineWidth(context, 20.0);

    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    NSArray *arrpoint = [dic objectForKey:@"points"];
    CGPoint startPoint = [[arrpoint objectAtIndex:0]CGPointValue];
    //设置线段的起始点
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    for (int i=1; i<arrpoint.count; i++) {
        CGPoint points= [[arrpoint objectAtIndex:i]CGPointValue];
        //循环添加点
        CGContextAddLineToPoint(context, points.x, points.y);
    }
    CGContextDrawPath(context, kCGPathStroke);
    
    //绘制路径
    CGContextStrokePath(context);
    CGContextEndPage(context);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //创建保存路径
    pathDic = [NSMutableDictionary dictionary];
    //设置当前的画笔颜色
    if (_storkeColor) {
        [pathDic setObject:_storkeColor forKey:@"color"];
    }
    else
    {
        [pathDic setObject:[UIColor blackColor] forKey:@"corlor"];
    }
    //画笔宽度
    if (_storkeWidth) {
        //KUANDU
        [pathDic setObject:@(_storkeWidth) forKey:@"width"];
    }
    else
    {
        [pathDic setObject:@(3.0) forKey:@"width"];
    }
    //类型
    [pathDic setObject:@(_storkeType) forKey:@"type"];
   
    //创建保存当前路径的数组
    pointArray = [[NSMutableArray alloc]init];
    //D得到起始点
    [self getPoint:touches];
    //将点保存到 字典中
    [pathDic setObject:pointArray forKey:@"points"];
    //将一条路径的所有属性保存到数组中
    [allPathArray addObject:pathDic];
}

//得到触摸点的位置
- (void)getPoint:(NSSet *)touches
{
    //得到触摸对象
    UITouch *touche = [touches anyObject];
    //得到触摸点在指定视图中的坐标
    CGPoint points = [touche locationInView:self];
    
    [pointArray addObject:[NSValue valueWithCGPoint:points]];

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //更新路径 （添加一个点）
    //移除旧的路径
    [allPathArray removeLastObject];
    //给路径添加一个点
    [self getPoint:touches];
    //更新字典内容 路径的点坐标
    [pathDic setObject:pointArray forKey:@"points"];
    //将最新的路径添加到数组中
    [allPathArray addObject:pathDic];
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [allPathArray removeLastObject];
    [self getPoint:touches];
    [pathDic setObject:pointArray forKey:@"points"];
    [allPathArray addObject:pathDic];
    [self setNeedsDisplay];    
}


@end
